/*
	NanoStack: MCU software and PC tools for sensor networking.
		
	Copyright (C) 2006-2007 Sensinode Ltd.

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License along
	with this program; if not, write to the Free Software Foundation, Inc.,
	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

		Address:
		Sensinode Ltd.
		PO Box 1
		90571 Oulu, Finland

		E-mail:
		info@sensinode.com
*/


/* Standard includes. */
#include <stdlib.h>
#include <string.h>

/* Scheduler includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"

#include <sys/inttypes.h>

#include "stack.h"
//#include "socket.h"
#include "uart.h"
#include "rf.h"
#include "bus.h"

#ifdef HAVE_DMA
#include "dma.h"
#endif

#include "debug.h"


/*-----------------------------------------------------------*/
static void vSendingTask(int8_t *pvParameters );
static void vReceivingTask(int8_t *pvParameters );

static void SendToAgent(uint16_t val);
static uint16_t ReceiveFromAgent();
static void messageHandle(int16_t byte, char tmp[]);

uint16_t DELAY = 0;

sockaddr_t *agentAddress[5];

socket_t* sendingSocket = 0;
socket_t* receivingSocket = 0;
buffer_t* sendingBuf = 0;
buffer_t* receivingBuf = 0;

sockaddr_t dst_address = 
{
	ADDR_802_15_4_PAN_LONG,
	{
		0xFF, 0xFF, 0xFF, 0xFF, 
		0xFF, 0xFF, 0x00, 0x01
	},
	61619
};

sockaddr_t dst_address2 = 
{
	ADDR_802_15_4_PAN_LONG,
	{
		0xFF, 0xFF, 0xFF, 0xFF, 
		0xFF, 0xFF, 0x00, 0x02
	},
	61618
};

sockaddr_t dst_address3 = 
{
	ADDR_802_15_4_PAN_LONG,
	{
		0xFF, 0xFF, 0xFF, 0xFF, 
		0xFF, 0xFF, 0x00, 0x03
	},
	61617
};

sockaddr_t dst_address4 = 
{
	ADDR_802_15_4_PAN_LONG,
	{
		0xFF, 0xFF, 0xFF, 0xFF, 
		0xFF, 0xFF, 0x00, 0x04
	},
	61616
};

sockaddr_t dst_address5 = 
{
	ADDR_802_15_4_PAN_LONG,
	{
		0xFF, 0xFF, 0xFF, 0xFF, 
		0xFF, 0xFF, 0x00, 0x05
	},
	61615
};

sockaddr_t src_address = 
{
	ADDR_802_15_4_PAN_LONG,
	{
		0xFF, 0xFF, 0xFF, 0xFF, 
		0xFF, 0xFF, 0x00, 0x00
	},
	61620
};
// Developed by Seung Hwan Shin(shshin@networking.khu.ac.kr)

int main()
{
	bus_init();
	
	stack_init();
	debug_init(115200);
	xTaskCreate( vReceivingTask, "Receiving", configMAXIMUM_STACK_SIZE, NULL, ( tskIDLE_PRIORITY + 0 ), ( xTaskHandle * )NULL );
	xTaskCreate( vSendingTask, "Sending", configMAXIMUM_STACK_SIZE, NULL, ( tskIDLE_PRIORITY + 0 ), ( xTaskHandle * )NULL );	

	vTaskStartScheduler();

	return 0;
}

//
static void vReceivingTask(int8_t *pvParameters)
{
	uint16_t value = 0;
	debug("Receiving task start.\r\n");
	
	receivingSocket = socket(MODULE_CUDP, 0);
	socket_bind(receivingSocket, &src_address);


	for(;;){
		value = ReceiveFromAgent();
	}
}
//
static uint16_t ReceiveFromAgent()
{
	uint16_t temp = 0;
	receivingBuf = socket_read(receivingSocket, 10);
	if(receivingBuf){
		LED1_OFF();
		debug_hex(ADCCON1);
		debug_hex(ADCCON2);
		debug_hex(ADCCON3);
		debug_hex(ADCCFG);
		temp = (uint16_t)(receivingBuf->buf[receivingBuf->buf_ptr]) << 8;
		temp |= receivingBuf->buf[receivingBuf->buf_ptr+1];

		debug_int(temp);
		debug(" recv.\r\n");
		LED1_ON();
	}
	socket_buffer_free(receivingBuf);
	receivingBuf = 0;
	return temp;
}

static void vSendingTask(int8_t *pvParameters )
{
	uint16_t policy = 3000;
	
	sendingSocket = socket(MODULE_CUDP, 0);
	sendingBuf = socket_buffer_get(sendingSocket);
	
	debug("Sending task start.\r\n");
		
	for(;;){		
		vTaskDelay(10000/portTICK_RATE_MS);
		SendToAgent(policy);
	}
	
	// int16_t byte;	
	// int i, j;
	// int val = 2000;
	// int count;
	// char *tarAgent;
	// char *cmd;
	// char *value;
	// char tmp[32];
	
	// agentAddress[0] = &dst_address;
	// agentAddress[1] = &dst_address2;
	// agentAddress[2] = &dst_address3;
	// agentAddress[3] = &dst_address4;
	// agentAddress[4] = &dst_address5;

	// count = 0;
	// debug("vSendingTask Start!!!"); 
	// for( ;; ){	
		// byte = debug_read_blocking(10);		
		// if(byte != -1){
			// switch(count){
				// case 0 :	// Target Agent
				// {
					// messageHandle(byte, tmp);
					// tarAgent = tmp;
					// count++;	// cur byte == '|';
					// break;
				// }
				// case 1 :	// Command
				// {
					// messageHandle(byte, tmp);
					// cmd = tmp;
					// count++;	// cur byte == '|';
					// break;
				// }
				// case 2 :	// Value
				// {
					// messageHandle(byte, tmp);
					// value = tmp;
					// val = atoi(value);
					// debug_printf("Change Interval Value : %d\r\n", val);
					// SendToAgent(val);
					// count = 0;	// cur byte == '|';
					debug("end\r\n");
					// break;
				// }
				// default :
					// break;
			// }
		// }
	// }	
}

static void messageHandle(int16_t byte, char tmp[])
{
	int i = 0;
	while(byte != '|'){		
		tmp[i] = byte;
		byte = debug_read_blocking(10);
		i++;
		if(byte == -1)
			break;
	}
	tmp[i] = '\0';
	debug_printf("Value : %s\r\n", tmp);
}

static void SendToAgent(uint16_t policy)
{
	int retry = 0;
	
	sendingBuf->buf[sendingBuf->buf_end++] = (policy >> 8);
	sendingBuf->buf[sendingBuf->buf_end++] = (uint8_t) policy;
	
	LED2_OFF();
	if ((socket_sendto(sendingSocket, &dst_address, sendingBuf)==pdFALSE)&&(retry<3)){
		vTaskDelay(5/portTICK_RATE_MS);
		retry++;
	}
	if(retry>=3){
		debug("Failed sending.\r\n");
		socket_buffer_free(sendingBuf);
	}
	else{
		debug_int(policy);
		debug(" sent.\r\n");
	}
	LED2_ON();
/*
	if(test_socket){
		buffer_t *buffer = socket_buffer_get(test_socket);
		if (buffer){												
			buffer->buf[buffer->buf_end++] = (val >> 8);
			buffer->buf[buffer->buf_end++] = (uint8_t)val;
			
			if (socket_sendto(test_socket, agentAddress[0], buffer) == pdTRUE ){
				vTaskDELAY(10/portTICK_RATE_MS);
				// debug_printf("Command : %s, %s, %s", tarAgent, cmd, value);
				debug_printf("Sented Interval Value : %d\r\n", val);
			}
		}
		else{
			debug("No buffer.\r\n");
		}
	}
	else{
		debug("No test socket.\r\n");
	}
	*/
}

//Developed by Seung Hwan Shin(shshin@networking.khu.ac.kr)